﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using LiveEduWizard.Filter;

namespace LiveEduWizard
{
    public partial class Agenda : UserControl
    {
        private const int RowHeight = 90;

        private readonly NotifyCollectionChangedEventHandler CollectionChanged;

        private List<AgendaItem> agendaItems = new List<AgendaItem>();

        public Agenda()
        {
            CollectionChanged = new NotifyCollectionChangedEventHandler(items_CollectionChanged);
            
            InitializeComponent();
        }

        public void ScrollTo(AgendaViewModel agenda)
        {
            if (agenda == null)
            {
                Viewer.ScrollToVerticalOffset(0);
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    double offset = 0;

                    foreach (var agendaItem in agendaItems)
                    {
                        if (agendaItem.Agenda == agenda)
                        {
                            Viewer.ScrollToVerticalOffset(offset);
                            break;
                        }
                        else
                            offset += agendaItem.CurrentHeight;
                    }
                });
            }
        }

        public static readonly DependencyProperty FilterProperty = DependencyProperty.Register("Filter",
            typeof(IFilterRule), typeof(Agenda), new PropertyMetadata(null, new PropertyChangedCallback(OnFilterChanged)));

        public IFilterRule Filter
        {
            get { return (IFilterRule)this.GetValue(FilterProperty); }
            set { SetValue(FilterProperty, value); }
        }

        private static void OnFilterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((Agenda)d).SetFilter((IFilterRule)e.NewValue);
        }

        private void SetFilter(IFilterRule rule)
        {
            AgendaViewModel onTop = null;
            double offset = 0;

            foreach (var agendaItem in agendaItems)
            {
                offset += agendaItem.CurrentHeight;

                if (offset > Viewer.VerticalOffset)
                    break;
                
                onTop = agendaItem.Agenda;
            }

            foreach (var agendaItem in agendaItems)
            {
                agendaItem.ApplyFilter(rule);
            }

            ScrollTo(onTop);
        }

        public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items",
            typeof(ObservableCollection<AgendaViewModel>), typeof(Agenda), new PropertyMetadata(null, new PropertyChangedCallback(OnItemsChanged)));

        private ObservableCollection<AgendaViewModel> items;
        public ObservableCollection<AgendaViewModel> Items
        {
            get { return (ObservableCollection<AgendaViewModel>)this.GetValue(ItemsProperty); }
            set { SetValue(ItemsProperty, value); }
        }

        private static void OnItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
        {
            ((Agenda)d).SetItems((ObservableCollection<AgendaViewModel>)e.NewValue);
        }

        private void SetItems(ObservableCollection<AgendaViewModel> value)
        {
            if (items != value)
            {
                if (items != null)
                {
                    items.CollectionChanged -= CollectionChanged;
                }

                items = value;

                ItemsPanel.Children.Clear();
                agendaItems.Clear();

                if (items != null)
                {
                    items.CollectionChanged += CollectionChanged;

                    foreach (var item in items)
                    {
                        var agendaItem = new AgendaItem(item);
                        agendaItems.Add(agendaItem);
                        ItemsPanel.Children.Add(agendaItem);
                    }                    
                }

                SetLastItem();
            }
        }

        private void SetLastItem()
        {
            ItemsPanel.Children.Add(new Rectangle() { Height = 56 });
        }

        private void items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    for (int i = 0; i < e.NewItems.Count; i++)
                    {
                        var agendaItem = new AgendaItem((AgendaViewModel)e.NewItems[i]);
                        ItemsPanel.Children.Insert(ItemsPanel.Children.Count - agendaItems.Count + e.NewStartingIndex + i - 1, agendaItem);
                        agendaItems.Insert(i + e.NewStartingIndex, agendaItem);                        
                    }
                    break;
                case NotifyCollectionChangedAction.Remove:
                    for (int i = 0; i < e.OldItems.Count; i++)
                    {
                        ItemsPanel.Children.Remove(agendaItems[e.OldStartingIndex + i]);                        
                    }
                    agendaItems.RemoveRange(e.OldStartingIndex, e.OldItems.Count);
                    break;
                case NotifyCollectionChangedAction.Replace:
                    for (int i = 0; i < e.OldItems.Count; i++)
                    {
                        ItemsPanel.Children.Remove(agendaItems[e.OldStartingIndex + i]);                        
                    }
                    agendaItems.RemoveRange(e.OldStartingIndex, e.OldItems.Count);
                    for (int i = 0; i < e.NewItems.Count; i++)
                    {
                        var agendaItem = new AgendaItem((AgendaViewModel)e.NewItems[i]);
                        ItemsPanel.Children.Insert(ItemsPanel.Children.Count - agendaItems.Count + e.NewStartingIndex + i - 1, agendaItem);
                        agendaItems.Insert(i + e.NewStartingIndex, agendaItem);
                    }
                    break;
                case NotifyCollectionChangedAction.Reset:
                    ItemsPanel.Children.Clear();
                    agendaItems.Clear();
                    
                    foreach (var item in items)
                    {
                        var agendaItem = new AgendaItem(item);
                        agendaItems.Add(agendaItem);
                        ItemsPanel.Children.Add(agendaItem);
                    }

                    SetLastItem();
                    break;
            }
        }
    }
}
